package com.atguigu.serviceedu.controller;


import com.atguigu.commonutils.R;
import com.atguigu.serviceedu.entity.EduChapter;
import com.atguigu.serviceedu.entity.chapter.ChapterVo;
import com.atguigu.serviceedu.service.EduChapterService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author JY
 * @since 2021-01-19
 */
@Api(description="课程章节管理")
@RestController
@RequestMapping("/serviceedu/chapter")
//@CrossOrigin
public class EduChapterController {
    @Autowired
    private EduChapterService chapterService;

    //根据courseId得到章节列表
    @GetMapping("getListByCourseId/{courseId}")
    public R getListByCourseId(@PathVariable String courseId){
        List<ChapterVo> list = chapterService.getListByCourseId(courseId);
        return R.ok().data("items",list);
    }

    //新增章节
    @PostMapping("save")
    public R save(@RequestBody EduChapter chapter){
        chapterService.save(chapter);
        return R.ok();
    }

    //根据ID查询章节
    @GetMapping("getById/{id}")
    public R getById(@PathVariable String id){
        EduChapter chapter = chapterService.getById(id);
        return R.ok().data("item",chapter);
    }

    //更新
    @PostMapping("update")
    public R update(@RequestBody EduChapter chapter){
        chapterService.updateById(chapter);
        return R.ok();
    }

    //删除
    @DeleteMapping("delete/{id}")
    public R delete(@PathVariable String id){
        boolean flag = chapterService.deleteById(id);
        return flag?R.ok():R.error().message("删除失败");
    }
}

